﻿using System.Windows;
using Microsoft.Practices.Unity;
using Moop.DataLayer.Olap.Service;
using Moop.UI.Views;
using Moop.UI.ViewModels;
using Moop.UI.Common;
using System;
using System.Windows.Threading;
using System.Threading.Tasks;

//http://stackoverflow.com/questions/2232365/unity-wpf-injecting-a-datacontext-via-property-injection-to-a-child-control
//http://stackoverflow.com/questions/1735836/getting-unity-to-resolve-views-in-xaml
//http://stackoverflow.com/questions/1472498/wpf-global-aggEx-handler

namespace Moop.UI
{
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            //Reg Types
            RegisterTypes();

            //Global Exception Handlers
            //AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnAppDomainUnhandledException);
            //Dispatcher.UnhandledException += OnDispatcherUnhandledException;
            //Application.Current.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(Current_DispatcherUnhandledException);
            //TaskScheduler.UnobservedTaskException += new EventHandler<UnobservedTaskExceptionEventArgs>(TaskScheduler_UnobservedTaskException);

            //Create & Display MainView
            MainView mainWindow = TypeContainer.Instance.Resolve<MainView>();
            mainWindow.Show();
        }

        private void RegisterTypes()
        {
            TypeContainer.Register();

            TypeContainer.Instance.RegisterType<IAppContext, AppContext>(new InjectionConstructor());
            TypeContainer.Instance.RegisterType<ServerViewModel, ServerViewModel>();
        }


        #region Exception Handlers
        
        void OnAppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception ex = (Exception)e.ExceptionObject;

            if (ex != null)
            {
                string errorMessage = string.Format("An unhandled aggEx occurred: {0}", ex.Message);
                MessageBox.Show(errorMessage, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
            {
                string errorMessage = "An unhandled aggEx occurred, can not be cast...";
                MessageBox.Show(errorMessage, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }


        void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            string errorMessage = string.Format("An unhandled aggEx occurred: {0}", e.Exception.Message);
            MessageBox.Show(errorMessage, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            e.Handled = true;
        }
        

        void Current_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            string errorMessage = string.Format("An unhandled aggEx occurred: {0}", e.Exception.Message);
            MessageBox.Show(errorMessage, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            e.Handled = true;
        }


        void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
        {
            string errorMessage = string.Format("An unhandled aggEx occurred: {0}", e.Exception.Message);
            MessageBox.Show(errorMessage, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        }

        #endregion

    }
}
